package cn.edu.xust.service.impl;

import cn.edu.xust.Ro.MulHotelWtRo;
import cn.edu.xust.Ro.PieData;
import cn.edu.xust.util.PageUtils;
import cn.edu.xust.util.Query;
import cn.hutool.core.date.DateUtil;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.edu.xust.dao.WtdataDao;
import cn.edu.xust.entity.WtdataEntity;
import cn.edu.xust.service.WtdataService;


@Service("wtdataService")
public class WtdataServiceImpl extends ServiceImpl<WtdataDao, WtdataEntity> implements WtdataService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<WtdataEntity> page = this.page(
                new Query<WtdataEntity>().getPage(params),
                new QueryWrapper<WtdataEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public WtdataEntity getnew(Integer id) {
        QueryWrapper<WtdataEntity> wap = new QueryWrapper<WtdataEntity>().eq("deviceid", id).last("ORDER BY ID DESC limit 1");
        return baseMapper.selectOne(wap);

    }

    @Override
    public List<WtdataEntity> getByDeviceNameAndTimetag(List<String> deivcenamelist, String timetag) {
        QueryWrapper<WtdataEntity> wap = new QueryWrapper<WtdataEntity>().in("devicename", deivcenamelist).eq("timetypeflag", timetag);
        return baseMapper.selectList(wap);

    }


    /**
     * 固定 22.30-10.30 的数据
     *
     * @param date       2024-11-22
     * @param hour  不要这个了       22.30-10.30
     * @param devicename 00002024034154
     * @return WtdataEntity
     */
    @Override
    public WtdataEntity searchNewstInHourtime(String date, int hour, String devicename) {

        date = date + " 10:30:00";

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        LocalDateTime dateTime = LocalDateTime.parse(date, formatter);
        LocalDateTime dateTime2 = LocalDateTime.parse(date, formatter);

        dateTime = dateTime.minusHours(12);
        String hourdate = dateTime.format(formatter);
        String hourdate2 = dateTime2.format(formatter);
        QueryWrapper<WtdataEntity> wap = new QueryWrapper<WtdataEntity>().gt("ctime", hourdate).lt("ctime", hourdate2);
        wap.eq("devicename", devicename);
        List<WtdataEntity> list = baseMapper.selectList(wap);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(list.size() - 1);
    }

    @Override
    public List<MulHotelWtRo> getByHType(String hotelid, String type, String dateStr) {
        //拿到前一日
        LocalDate date = LocalDate.parse(dateStr);
        LocalDate previousDate = date.minusDays(1);
        String previousDateStr = previousDate.toString();
        return baseMapper.getByHType(hotelid, type, previousDateStr + " 22:30:00", date + " 10:30:00");
    }

    @Override
    public List<PieData> getByDay(String name, String start, String end) {
        return baseMapper.getByDay( name,  start,  end);
    }
}